/**
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

import core.effect.Tween;

class LinearMotion extends Tween {

    /**
     * @param target {Object} Object implementing <code>getX</code>, <code>getY</code> and <code>move(x, y)</code> methods.
     * @param duration {int} Effect duration, in milliseconds.
     * @param timer {Timer} Effect timer.
     * @param x {int} Coordinates of the motion endpoint.
     * @param y {int} Coordinates of the motion endpoint.
     * @param easing (core.effect.easing.*) Easing effect.
     * @param easingType {String} [optional] Easing type (default is Tween.EASE_IN).
     * @constructor
     */
    this.LinearMotion = function (target, duration, timer, x, y, easing, easingType) {
        super(duration, timer, 0, Math.sqrt(x * x + y * y), easing, easingType);
        this.target = target;
        this.startX = target.getX();
        this.startY = target.getY();
        this.endX = x;
        this.endY = y;
    };

    /**
     * Sets the target window.
     *
     * @param target {Object} Object implementing <code>getX</code>, <code>getY</code> and <code>move(x, y)</code> methods.
     */
    this.setTarget = function (target) {
        this.target = target;
    };

    /**
     * Gets the target window.
     *
     * @return Object
     */
    this.getTarget = function () {
        return this.target;
    };

    /**
     * @public
     * @param x {int} Start position coordinates.
     * @param y {int} Start position coordinates.
     */
    this.setStartPosition = function(x, y) {
        this.startX = x;
        this.startY = y;
        var dx = (this.endX - this.startX);
        var dy = (this.endY - this.startY);
        super.setEndPosition(Math.sqrt(dx * dx + dy * dy));
    };

    /**
     * @public
     * @return {int}
     */
    this.getStartPositionX = function() {
        return this.startX;
    };

    /**
     * @public
     * @return {int}
     */
    this.getStartPositionY = function() {
        return this.startY;
    };

    /**
     * @public
     * @param x {int} End position coordinates.
     * @param y {int} End position coordinates.
     */
    this.setEndPosition = function (x, y) {
        this.endX = x;
        this.endY = y;
        var dx = (this.endX - this.startX);
        var dy = (this.endY - this.startY);
        super.setEndPosition(Math.sqrt(dx * dx + dy * dy));
    };

    /**
     * @public
     * @return {int}
     */
    this.getEndPositionX = function() {
        return this.endX;
    };

    /**
     * @public
     * @return {int}
     */
    this.getEndPositionY = function() {
        return this.endY;
    };

    /**
     * @inheritDoc
     */
    this.play = function (time) {
        this.target.move(this.startX, this.startY);
        this.beginX = this.startX;
        this.beginY = this.startY;
        var distance = this.getEndPosition() - this.getStartPosition();
        this.cos = (distance > 0) ? (this.endX - this.beginX) / distance : 0;
        this.sin = (distance > 0) ? (this.endY - this.beginY) / distance : 0;
        super.play(time);
    };

    /**
     * @inheritDoc
     */
    this.playback = function () {
        var position = this.getPosition();
        this.target.move(Math.round(this.beginX + position * this.cos),
            Math.round(this.beginY + position * this.sin));
    };
}